//拍照触发监听
document.getElementById("changeImg").addEventListener("tap", function() {
	if(mui.os.plus) {

		var a = [{
				title: "拍照"
			},
			{
				title: "从手机相册选择"
			}
		]

		plus.nativeUI.actionSheet({
			title: "修改用户头像",
			cancel: "取消",
			buttons: a
		}, function(b) { //actionsheet 点击之后出现的东西
			switch(b.index) {
				case 0:
					break;
				case 1:
					getpic();
					break;
				case 2:
					getimg();
					break;
				default:
					break;
			}
		})
	}
})
//拍照
function getpic() {
	var cmr = plus.camera.getCamera();
	cmr.captureImage(function(p) {
		plus.io.resolveLocalFileSystemURL(p, function(entry) {
			compressImage(entry.toLocalURL(), entry.name, divid);
		}, function(e) {
			plus.nativeUI.toast("读取拍照文件错误：" + e.message);
		});
	}, function(e) {}, {
		filename: "_doc/camera/",
		index: 1
	});
}

//获取相册
function getimg() {
	plus.gallery.pick(function(a) {
		plus.io.resolveLocalFileSystemURL(a, function(entry) {
			plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
				root.getFile("head.png", {}, function(file) {
					//文件已存在
					file.remove(function() {
						console.log("删除成功");
						entry.copyTo(root, "head.png", function(e) {
							var e = e.fullPath + "?version=" + new Date().getTime();
							uploadHead(e); /*上传图片*/
							//变更大的图预览的src
							//目前只有一张图暂且这样处理，后续需要用标准组件实现
						}, function(e) {
							console.log("copy img file:" + e.message);
						});
					}, function() {
						console.log("删除" + e.message)
					});
				}, function() {
					//文件不存在
					entry.copyTo(root, "head.png", function(e) {
						var path = e.fullPath + "?version=" + new Date().getTime();
						uploadhead(path); /*上传图片*/
					}, function(e) {
						console.log("copy img file:" + e.message);
					});
				});
			}, function(e) {
				console.log("get _www folder fail");
			})
		}, function(e) {
			console.log("读取拍照文件错误" + e.message);
		})
	}, function(a) {}, {
		filter: "image"
	})
}
//上传头像图片
function uploadHead(imgPath) {
	var mainImage = document.getElementById("headImg");
	mainImage.src = imgPath;

	var image = new Image();
	image.src = imgPath;
	image.onload = function() {
		var imgData = getBase64Image(image);

		$.ajax({
			type: "post",
			data: imgData,
			url: "http://10.8.165.31/ceshi/reuserimg.php",
			success: function(data) {
				console.log("ajax成功")
				console.log(data);
			},
			error: function() {
				console.log("ajax失败")
			}
		});
	}
}

//转换64
function getBase64Image(img) {
	var canvas = document.createDocument("canvas");
	var width = img.width;
	var height = img.height;

	if(width > height) {
		if(width > 100) {
			height = Math.round(height *= 100 / width);
			width = 100;
		}
	} else {
		if(height > 100) {
			width = Math.round(width *= 100 / height);
			height = 100;
		}
	}

	canvas.width = width;
	canvas.height = height;

	var ctx = canvas.getContext("2d");
	ctx.drawImage(img, 0, 0, width, height); /*绘图*/
	var dataURL = canvas.toDataURL("image/png", 0.8);
	return dataURL.replace("data:image/png;base64,", "");

}

//选择性别
var showSex = document.getElementById('showSex');
var picker = new mui.PopPicker();
picker.setData([{
	value: 'man',
	text: '男'
}, {
	value: 'woman',
	text: '女'
}]);
mui('.sex').on('tap', 'a', function() {
	console.log(123);
	picker.show(function(selectItems) {
		showSex.innerText = selectItems[0].text;
	})
});

//选择日期
var birthDate = document.getElementById('birthDate');
mui('.birth').on('tap', 'a', function() {
	var dtpicker = new mui.DtPicker({
		type: "date", //设置日历初始视图模式 
		beginDate: new Date(1900, 01, 01), //设置开始日期 
		endDate: new Date(2020, 11, 30), //设置结束日期 
		labels: ['年', '月', '日'] //设置默认标签区域提示语 
	})
	dtpicker.show(function(e) {
		console.log(e);
		birthDate.innerText = e.text;

	})
});

//选择地区
mui.plusReady(function() {
	document.getElementById('showAddress').addEventListener('tap', function() {

		var adressStr = '';
		//显示几层，我们要显示省、市、区，所以我们这里写三层，如果不写默认是一层
		var addressPicker = new mui.PopPicker({
			layer: 3
		});
		addressPicker.setData(cityData3);
		//city.data-3.js中的数据

		addressPicker.show(function(selectItems) {
			//将选择的省、市、区显示到屏幕上
			for(var i = 0; i < selectItems.length; i++) {
				adressStr += selectItems[i].text;
			}
			document.getElementById('showAddress').innerHTML = adressStr;
		});
	})
});